package com.weisy.signin.ui.fragment;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.weisy.signin.R;
import com.weisy.signin.adapter.CommAdapter;
import com.weisy.signin.base.BaseFragment;
import com.weisy.signin.bean.Plan;
import com.weisy.signin.comm.Comm;
import com.weisy.signin.widget.MsgDialog;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RequestExecutor;
import com.yinshan.calendar.utils.DateTimeHelper;
import com.yinshan.qrcode.activity.CaptureActivity;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import androidx.core.content.ContextCompat;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author weisy
 * @function 日程
 * @date 2019/3/21 21:22
 */
public class PlanFragment extends BaseFragment {
    @BindView(R.id.txt_title1)
    TextView txtTitle1;
    @BindView(R.id.txt_title2)
    TextView txtTitle2;
    @BindView(R.id.txt_title3)
    TextView txtTitle3;
    @BindView(R.id.txt_null_1)
    TextView txtNull1;
    @BindView(R.id.txt_null_2)
    TextView txtNull2;
    @BindView(R.id.txt_null_3)
    TextView txtNull3;
    @BindView(R.id.list_1)
    ListView listv1;
    @BindView(R.id.list_2)
    ListView listv2;
    @BindView(R.id.list_3)
    ListView listv3;

    private CommAdapter<Plan> adapter1;
    private ArrayList<Plan> plans1 = new ArrayList<>();
    private CommAdapter<Plan> adapter2;
    private ArrayList<Plan> plans2 = new ArrayList<>();
    private CommAdapter<Plan> adapter3;
    private ArrayList<Plan> plans3 = new ArrayList<>();

    private String date1;
    private String date2;
    private String date3;

    @Override
    public int getLayoutId() {
        return R.layout.frag_plan;
    }

    @Override
    public void initViews(View view) {
        Calendar c = Calendar.getInstance();
        date2 = DateTimeHelper.formatToString(c.getTime(), DateTimeHelper.FORMAT_DAY);

        c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) - 1);
        date1 = DateTimeHelper.formatToString(c.getTime(), DateTimeHelper.FORMAT_DAY);

        c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) + 2);
        date3 = DateTimeHelper.formatToString(c.getTime(), DateTimeHelper.FORMAT_DAY);

        txtTitle1.setText(String.format("昨日活动(%s)", date1));
        txtTitle2.setText(String.format("今日安排(%s)", date2));
        txtTitle3.setText(String.format("明日预告(%s)", date3));

        adapter1 = new CommAdapter<Plan>(plans1, R.layout.layout_item_plan) {
            @Override
            public void bindView(ViewHolder holder, Plan obj) {
                holder.setText(R.id.txt_item_plan_title, obj.getTitle());
                holder.setText(R.id.txt_item_plan_time, "时间:" + obj.getDate());
                holder.setText(R.id.txt_item_plan_local, "地点:" + obj.getLocal());
            }
        };
        listv1.setAdapter(adapter1);
        listv1.setOnItemClickListener((parent, view1, position, id) -> {
            ARouter.getInstance().build(Comm.ROUTE_PLAN_INFO).withParcelable("plan", plans1.get(position)).navigation();
        });
        listv1.setOnItemLongClickListener((parent, view12, position, id) -> {
            showDialog(plans1.get(position));
            return false;
        });
        adapter2 = new CommAdapter<Plan>(plans2, R.layout.layout_item_plan) {
            @Override
            public void bindView(ViewHolder holder, Plan obj) {
                holder.setText(R.id.txt_item_plan_title, obj.getTitle());
                holder.setText(R.id.txt_item_plan_time, "时间:" + obj.getDate());
                holder.setText(R.id.txt_item_plan_local, "地点:" + obj.getLocal());
            }
        };
        listv2.setAdapter(adapter2);
        listv2.setOnItemClickListener((parent, view1, position, id) -> {
            ARouter.getInstance().build(Comm.ROUTE_PLAN_INFO).withParcelable("plan", plans2.get(position)).navigation();
        });
        listv2.setOnItemLongClickListener((parent, view12, position, id) -> {
            showDialog(plans2.get(position));
            return false;
        });
        adapter3 = new CommAdapter<Plan>(plans3, R.layout.layout_item_plan) {
            @Override
            public void bindView(ViewHolder holder, Plan obj) {
                holder.setText(R.id.txt_item_plan_title, obj.getTitle());
                holder.setText(R.id.txt_item_plan_time, "时间:" + obj.getDate());
                holder.setText(R.id.txt_item_plan_local, "地点:" + obj.getLocal());
            }
        };
        listv3.setAdapter(adapter3);
        listv3.setOnItemClickListener((parent, view1, position, id) -> {
            ARouter.getInstance().build(Comm.ROUTE_PLAN_INFO).withParcelable("plan", plans3.get(position)).navigation();
        });
        listv3.setOnItemLongClickListener((parent, view12, position, id) -> {
            showDialog(plans3.get(position));
            return false;
        });
    }

    @Override
    public void initDatas() {

    }

    @Override
    public void onResume() {
        super.onResume();
        AndPermission.with(context)
                .runtime()
                .permission(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE)
                .onGranted(granted -> {
                    updateView();
                })
                .onDenied(denied -> {
                    ToastUtils.show("必要权限缺失");
                }).start();
    }

    private void updateView() {
        List<Plan> tmp1 = LitePal.where("date=?", date1).find(Plan.class);
        plans1.clear();
        if (null != tmp1 && !tmp1.isEmpty()) {
            plans1.addAll(tmp1);
            adapter1.notifyDataSetChanged();
        }
        List<Plan> tmp2 = LitePal.where("date=?", date2).find(Plan.class);
        plans2.clear();
        if (null != tmp2 && !tmp2.isEmpty()) {
            plans2.addAll(tmp2);
            adapter2.notifyDataSetChanged();
        }
        List<Plan> tmp3 = LitePal.where("date=?", date3).find(Plan.class);
        plans3.clear();
        if (null != tmp3 && !tmp3.isEmpty()) {
            plans3.addAll(tmp3);
            adapter3.notifyDataSetChanged();
        }
        if (plans1.isEmpty()) {
            txtNull1.setVisibility(View.VISIBLE);
        } else {
            txtNull1.setVisibility(View.GONE);
        }
        if (plans2.isEmpty()) {
            txtNull2.setVisibility(View.VISIBLE);
        } else {
            txtNull2.setVisibility(View.GONE);
        }
        if (plans3.isEmpty()) {
            txtNull3.setVisibility(View.VISIBLE);
        } else {
            txtNull3.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.img_plan_add, R.id.txt_title1, R.id.txt_title2, R.id.txt_title3})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_plan_add:
                ARouter.getInstance().build(Comm.ROUTE_ADD_PLAN).navigation();
                break;
            case R.id.txt_title1:
                ARouter.getInstance().build(Comm.ROUTE_PLAN_LIST).withString("day", date1).navigation();
                break;
            case R.id.txt_title2:
                ARouter.getInstance().build(Comm.ROUTE_PLAN_LIST).withString("day", date2).navigation();
                break;
            case R.id.txt_title3:
                ARouter.getInstance().build(Comm.ROUTE_PLAN_LIST).withString("day", date3).navigation();
                break;

        }
    }

    private void showDialog(Plan plan) {
        new MsgDialog.Builder(context)
                .setCenter(true)
                .setMsg("您确定要删除该条记录吗？")
                .setBtnColor(ContextCompat.getColor(context, R.color.colorBlue))
                .setTxtLeft("取消", (dialog, which) -> dialog.dismiss())
                .setTxtRight("确定", (DialogInterface.OnClickListener) (dialog, which) -> {
                    dialog.dismiss();
                    if (plan.delete() > 0) {
                        ToastUtils.show("删除成功");
                        updateView();
                    } else {
                        ToastUtils.show("删除失败");
                    }
                }).create().show();
    }
}
